package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import persistence.PerformanceEngineDAO;
import persistence.PerformanceVO;

/**
 * Servlet implementation class getquotes
 */
public class performance extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public performance() {
        super();
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PerformanceEngineDAO dao = new PerformanceEngineDAO();
		PerformanceVO vo = new PerformanceVO();
		if(request.getParameter("action").equals("ticket")){
			vo.setDatelimit(request.getParameter("datelimit"));
			vo.setDate(request.getParameter("dateorder"));
			vo.setStock(request.getParameter("stock").trim());
			vo.setBuysell((request.getParameter("status").equals("buy")) ? 1 : 2);
			vo.setBook(request.getParameter("book"));
			vo.setPbuy(request.getParameter("pbuy").replace(",", "."));
			vo.setPstoploss(request.getParameter("pstoploss").replace(",", "."));
			vo.setPlimit(request.getParameter("plimit").replace(",", "."));
			dao.persist(vo);
	
			response.sendRedirect("/stockblog/pages/"+request.getParameter("page")+".jsp?date="+request.getParameter("date"));
		}
		if(request.getParameter("action").equals("sellorder")) {
			dao.update(Integer.parseInt(request.getParameter("id")), 3, request.getParameter("dateorder"), request.getParameter("psell"));
			response.sendRedirect("/stockblog/pages/"+request.getParameter("page")+".jsp?date="+request.getParameter("date"));
		}		

		if(request.getParameter("action").equals("deleteorder")){
			dao.delete(Integer.parseInt(request.getParameter("id")));
			response.sendRedirect("/stockblog/pages/"+request.getParameter("page")+".jsp?date="+request.getParameter("date"));
		}		
	}

}
